package cn.wolfcode.p2p.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.IAccountService;
import cn.wolfcode.p2p.base.service.IRealAuthService;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.query.BidRequestQuery;
import cn.wolfcode.p2p.business.query.CreditTransferQuery;
import cn.wolfcode.p2p.business.service.IBidRequestService;
import cn.wolfcode.p2p.business.service.IBidService;
import cn.wolfcode.p2p.business.service.ICreditTransferService;
import cn.wolfcode.p2p.util.Constants;
import cn.wolfcode.p2p.util.JsonResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
public class InvestController {
    @Autowired
    private IBidRequestService bidRequestService;
    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IAccountService accountService;
    @Autowired
    private IRealAuthService realAuthService;
    @Autowired
    private IBidService bidService;
    @Autowired
    private ICreditTransferService creditTransferService;

    /**
     * 投资列表页面,无需查询数据
     *
     * @return
     */

    @RequestMapping("invest")
    public String invest() {
        return "invest/invest";
    }

    @RequestMapping("invest_list")
    public String investList(Model model, @ModelAttribute("qo") BidRequestQuery qo) {
        if (qo.getBidRequestType() == Constants.BIDREQUEST_TYPE_NORMAL) {
            if (qo.getBidRequestState() == -1) {
                //设置已完成/招标中
                qo.setBidRequestStates(new Integer[]{Constants.BIDREQUEST_STATE_BIDDING, Constants.BIDREQUEST_STATE_COMPLETE_PAY_BACK});
            }
            model.addAttribute("pageInfo", bidRequestService.query(qo));
            return "invest/invest_list";

        } else {
            //如果是 bidRequestType=1 查询债权标
            CreditTransferQuery cqo = new CreditTransferQuery();
            cqo.setPageSize(qo.getPageSize());
            cqo.setCurrentPage(qo.getCurrentPage());
            cqo.setBidRequestState(Constants.BIDREQUEST_STATE_BIDDING);
            model.addAttribute("pageInfo", creditTransferService.queryCanCreditTransfer(cqo));
            return "credittransfer/credittransfer_list";
        }
    }

    @RequestMapping("borrow_info")
    public String borrowInfo(Model model, Long id) {
        BidRequest bidRequest = bidRequestService.getById(id);
        model.addAttribute("bidRequest", bidRequest);

        //借款人
        LoginInfo loginInfo = bidRequest.getCreateUser();
        UserInfo userInfo = userInfoService.getById(loginInfo.getId());
        model.addAttribute("userInfo", userInfo);

        //判断是否是登录用户
        LoginInfo currentUser = UserContext.getCurrentUser();
        if (currentUser != null) {
            if (currentUser.getId().longValue() == loginInfo.getId().longValue()) {
                //说明是借款用户
                model.addAttribute("self", true);
            } else {
                //说明是投资人的账户
                model.addAttribute("account", accountService.getById(currentUser.getId()));
            }
        }

        //借款人的实名信息
        model.addAttribute("realAuth", realAuthService.getById(userInfo.getRealAuthId()));
        return "borrow/borrow_info";
    }

    /**
     * 投标
     *
     * @return
     */
    @RequestMapping("borrow_bid")
    @ResponseBody
    public JsonResult borrowBid(Long bidRequestId, BigDecimal amount) {
        JsonResult jsonResult = new JsonResult();
        try {
            bidRequestService.bid(bidRequestId, amount);
        } catch (CustomerException e) {
            jsonResult.setMessage(e.getMessage());
            throw new CustomerException(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("系统繁忙中,请稍后再试");
        }
        return jsonResult;
    }

}
